<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <title>layui</title>
  <meta name="renderer" content="webkit">
  <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
  <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
  <link rel="stylesheet" href="../res/layui/css/layui.css"  media="all">
  <!-- 注意：如果你直接复制所有代码到本地，上述css路径需要改成你本地的 -->
  <script type="text/javascript">
      $.ajaxSetup({
          async: false
      });
  </script>
</head>
<body>
<fieldset class="layui-elem-field layui-field-title" style="margin-top: 50px;">
  <legend>增加和修改</legend>
</fieldset>
 
<form class="layui-form" action="" lay-filter="example">
	<input type='hidden' name='moduleId' id='moduleId'>
  <div class="layui-form-item">
    <label class="layui-form-label">上级模块名</label>
    <div class="layui-input-block">
      <input type="text" name="modModuleId" id="modModuleId" lay-filter="modModuleId" autocomplete="off" placeholder="请输入标题" class="layui-input" id="modmoduleId" value="0">
    </div>
  </div>
  <div class="layui-form-item">
    <label class="layui-form-label">模块编码</label>
    <div class="layui-input-block">
      <input type="text" name="standbyTwo" lay-verify="title" id="standbyTwo" placeholder="请输入模块编码" autocomplete="off" class="layui-input">
    </div>
  </div>
  <div class="layui-form-item">
    <label class="layui-form-label">模块名</label>
    <div class="layui-input-block">
      <input type="text" name="moduleName" lay-verify="title" id="moduleName" placeholder="请输入模块名" autocomplete="off" class="layui-input">
    </div>
  </div>
  <div class="layui-form-item">
    <label class="layui-form-label">模块地址</label>
    <div class="layui-input-block">
      <input type="text" name="moduleAddress" id="moduleAddress" autocomplete="off" placeholder="请输入模块地址" class="layui-input">
    </div>
  </div>
  <div class="layui-form-item">
    <label class="layui-form-label">请选择类型</label>
    <div class="layui-input-block" id="block">
      <select lay-filter="standbyOne" id="standbyOne" name="standbyOne">
        <option value="">请选择</option>
        <option value="0" id="0">菜单</option>
        <option value="1" id="1">按钮</option>
      </select>
    </div>
  </div>

  <div class="layui-form-item layui-form-text">
    <label class="layui-form-label">备注</label>
    <div class="layui-input-block">
      <textarea placeholder="请输入备注信息" class="layui-textarea" lay-verify="title" name="moduleNote" id="moduleNote"></textarea>
    </div>
  </div> 
  <div class="layui-form-item">
    <div class="layui-input-block">
      <button type="submit" class="layui-btn" lay-submit="" lay-filter="demo1">立即提交</button>
    </div>
  </div>
   
</form>
<script src="../res/layui/layui.js" charset="utf-8"></script>
<script src="../res/js/jquery-3.4.1.min.js" charset="utf-8"></script>
<script src="../res/js/tools.js" charset="utf-8"></script>
<!-- 注意：如果你直接复制所有代码到本地，上述js路径需要改成你本地的 -->
<script>
layui.config({
    base: "../../pages/atsBaseItem/module/"
}).extend({
    treeSelect: "treeSelect/treeSelect"
}).use(['treeSelect','form', 'layedit', 'laydate','upload'], function(){
  var form = layui.form
  ,layer = layui.layer
  ,layedit = layui.layedit
  ,laydate = layui.laydate
  ,upload = layui.upload
  ,treeSelect= layui.treeSelect;

    treeSelect.render({
        // 选择器
        elem: '#modModuleId',
        // 数据
        data:'../../Module/findByAll' ,
        // 请求头
        headers: {},
        // 异步加载方式：get/post，默认get
        type: 'get',
        // 占位符
        placeholder: '不选择默认为顶级',
        //连线
        showLine: true,
        // 点击回调
        click: function(d){
            console.log(d);
        },
        // 加载完成后的回调函数
        success: function (d) {
            console.log(d);
        }
    });
  //创建一个编辑器
  var editIndex = layedit.build('LAY_demo_editor');
  //自定义验证规则
  form.verify({
    title: function(value){
      if(value.length < 2){
        return '标题至少得2个字符啊';
      }
    }
    ,pass: [
      /^[\S]{6,12}$/
      ,'密码必须6到12位，且不能出现空格'
    ]
    ,content: function(value){
      layedit.sync(editIndex);
    }
  });
  //监听提交
  form.on('submit(demo1)', function(data){
    var url = "../../Module/insert";
    $.post(url,data.field,function(obj){
    	if(obj.state==1){
    		layer.msg(obj.msg,{
				icon:1,
			 	time:1000,
			 },function(){
				 var indexs = parent.layer.getFrameIndex(window.name);
				 parent.layer.close(indexs);
			 });
    		return false ;
    	}
    },"json")
    return false;
  });
  var id = getQueryString("id");
  if(id!=null){
	  var url = "../../Module/findId";
		var data = {moduleId:id};
		$.post(url,data,function(obj){
            $("#treeSelect-input-lhc1234").val(obj.modmoduleName);
            $("#modModuleId").val(obj.modmoduleId);
            $("#standbyOne").val(obj.standbyOne);
            if(obj.standbyOne==0){
                $("#block input").val("菜单");
            }else{
                $("#block input").val("按钮");
            }
            $("#standbyTwo").val(obj.standbyTwo);
			$("#moduleId").val(obj.moduleId);
			$("#moduleName").val(obj.moduleName);
			$("#moduleAddress").val(obj.moduleAddress);
			$("#moduleNote").val(obj.moduleNote);
		},'json')
  }
});
</script>
</body>
</html>